<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: HCZ <huangchengze@etrol.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\Admin;
use app\admin\controller\api\Role as RoleApi;
/**
 * 角色管理
 * @package app\rule
 */
class Role  extends Admin

{

	protected $api;

	public function _initialize()
	{
		parent::_initialize();
		$this->api = new RoleApi();
	}
	
	/**
	 * 首页
	 * @return View
	 */
	public function index() {
		if(IS_AJAX){
			return $this->api->roleList();
		}
		return $this->fetch();
	}
	
	/**
	 * 查询
	 * @return Json
	 */
	public function search() {
		if(IS_AJAX){
			$roleList = $this->api->search(input('keyword'));
			return $roleList;
		}
	}
	
	/**
	 * 新增规则
	 * @return View
	 */
	public function add(){
		if(IS_POST){
			$data = input('post.');
			return $this->api->add($data);
		}
		$areaList = $this->api->getAreaTree()->getData();
		$ruleList = $this->api->getRuleList()->getData();
		$this->assign('areaList',$areaList['data']);
		$this->assign('ruleList',$ruleList['data']);
		return $this->fetch();
	}
	
	/**
	 * 编辑规则
	 * @param string $ruleId 规则ID
	 * @return View
	 */
	public function edit($roleId = NULL){
		if(IS_POST){
			$data = input('post.');
			return $this->api->edit($data,$roleId);
		}
		//园区分区
		$areaList = $this->api->getAreaTree()->getData();
		//权限列表
		$ruleList = $this->api->getRuleList()->getData();
		//获取菜单信息
		$roleInfo = model('Role')->find($roleId);
		//角色分区
		$this->assign('areaList',$areaList['data']);
		$this->assign('ruleList',$ruleList['data']);
     	$this->assign('info',$roleInfo);
		return $this->fetch();
	}

	/**
	 * 删除规则
	 * return Json
	 */
	public function del($roleId){
		if(IS_GET){
			return $this->api->del($roleId);
		}
	}

}